<template>
  <div id="register">
    <div class="header">
      <el-page-header @back="goBack" content="注册账号">
      </el-page-header>
    </div>
    <div class="content">
      <h2 class="content_title">注册账号</h2>
      <!-- 注册表单 -->
        <el-form class="RegisterForm"
                 :model="registerForm"
                 :rules="rules"
                 :inline="false"
                 ref="registerForm"
                 >
          <el-form-item prop="email">
            <el-input placeholder="用户邮箱号"
                      prefix-icon="el-icon-message"
                      v-model="registerForm.email">
            </el-input>
          </el-form-item>

          <el-form-item prop="password">
            <el-input placeholder="设置密码"
                      prefix-icon="el-icon-unlock"
                      show-password
                      v-model="registerForm.password">
            </el-input>
          </el-form-item>

          <el-form-item prop="captcha">
            <el-input placeholder="验证码"
                      prefix-icon="el-icon-s-comment"
                      style="width: 55%"
                      v-model="registerForm.captcha">
            </el-input>
            <el-button type="primary"
                       size="medium"
                       style="margin-left: 20px;"
                       @click="sendCaptcha(registerForm)">发送验证码</el-button>
          </el-form-item>

          <el-form-item>
            <el-button type="primary"
                       size="medium"
                       style="width: 100%"
                       @click="registerSubmit(registerForm)">注 册</el-button>
          </el-form-item>

        </el-form>
    </div>
  </div>
</template>

<script>
import { sendEmail, register } from 'api/login/login.js'
export default {
  data() {
    return{
      registerForm:{
        email: '',
        password: '',
        captcha: ''
      },
      rules: {
        email: [
          { required: true, message: '请输入邮箱账号', trigger: 'blur' },
          {
            min: 3,
            max: 30,
            type:'email',
            message: '请输入正确的邮箱号',
            trigger:[ 'blur','change']
          }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            min: 3,
            max: 15,
            message: '设置长度在 3 到 15 个字符的密码',
            trigger:['blur','change']
          }
        ],
        captcha: [
           { required: true, message: '请输入验证码', trigger: 'blur' },
        ]
      },
    }
  },
  components:{

  },
  methods:{
    goBack() {
      this.$router.push({ name: 'Login' })
    },
    // 发送邮箱验证码
    sendCaptcha(registerForm) {
      const params = {
        email: registerForm.email,
        type: 1
      }
      const email = params.email;
      if (!email) {
        return  this.$message.warning('请输入邮箱账号')
     }
    if (!/^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$/.test(email)) {
      return this.$message.warning('请输入正确的邮箱格式')
   }
      sendEmail(params).then(res => {

      }).catch(error=>{
        this.$message.error('发送失败，详细查看控制台')
        console.log(error)
      })
    },
    //提交注册账号
    registerSubmit(registerForm) {
      this.$refs.registerForm.validate((valid)=>{
        if(valid){
            register(registerForm)
      .then(res => {
        const resData = res.data
        if(resData.success) {
          this.$message({
            message: resData.message,
            type: 'success'
          })
          this.$router.push({ name: 'Login' })
        }else{
          this.$message({
            message: resData.message,
            type: 'error'
          })
        }
      })
      .catch(() => {
          this.$message({
              message: '无法连接到服务器',
              type: 'error'
          })
      })
        }
      })
    
  }
}
}
</script>

<style lang="scss" scoped>
#register{
  .header{
    user-select: none;
    width: 100%;
    margin: 5px 5px;
  }
  .content{
    width: 500px;
    height: 400px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    .content_title{
      text-align: center;
    }
    .RegisterForm{
      width: 300px;
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
    }
  }
}

</style>
